package com.sunmouse.toolkit.fragment.web;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunmouse.toollib.utils.FileUtils;
import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toollib.utils.system.PermissionUtil;
import com.sunmouse.toollib.widget.SimpleWebView;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.fragment.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CssFragment extends BaseFragment {


    @BindView(R.id.m_web)
    SimpleWebView mWeb;
    Unbinder unbinder;

    public CssFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_css, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
//        String site = "file:///android_asset/html/index.html";
        String site = "file:///" + FileUtils.getStoragePath() + "/resola/reesola/html/project.html";
        LogUtils.d("-------" + site);
//        String site = "http://img4.imgtn.bdimg.com/it/u=1074895912,3051340368&fm=15&gp=0.jpg";
        this.mWeb.loadUrl(site);

        String[] permissions = new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        };

        if (!PermissionUtil.isPermitted(getActivity(), permissions)) {
            PermissionUtil.requestPermission(getActivity(), permissions, 10);
            return;
        }
        this.mWeb.setCatheDirctory(getActivity().getCacheDir().getAbsolutePath());
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            this.mWeb.setCatheDirctory(getActivity().getCacheDir().getAbsolutePath());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
